package com.nulldev.util.java.swing.extSwing;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import com.nulldev.util.java.JVMUtil;
import com.nulldev.util.java.swing.SwingUtil;
import com.nulldev.util.java.swing.SwingUtil.ScrollDirection;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.Theme;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.ThemeApplier;
import com.nulldev.util.java.swing.font.FontManager;
import com.nulldev.util.math.MathUtil;
import com.nulldev.util.threading.legacy.ThreadExecutor;

import javax.swing.JProgressBar;
import javax.swing.JScrollBar;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import java.awt.Toolkit;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

public class eActiveConsoledBar extends JFrame {

	private JPanel contentPane;
	private JProgressBar progressBar;

	private boolean indter;
	private int x = 0;
	private boolean countUP = true;
	private boolean haltF;
	private final Runnable r = new Runnable() {

		@Override
		public void run() {
			while (true) {
				if (haltF) {
					break;
				}
				try {
					if (indter) {
						if (x >= progressBar.getMaximum()) {
							countUP = false;
						} else if (x <= progressBar.getMinimum()) {
							countUP = true;
						}
						if (countUP) {
							x += 1;
							progressBar.setValue(x);
						} else {
							x -= 1;
							progressBar.setValue(x);
						}
					}
					Thread.sleep(SwingUtil.FPS_60);
				} catch (Exception e) {
				}
			}
		}
	};
	private JLabel lblLoading;
	private JScrollPane scrollPane;
	private JTextPane textPane;
	public boolean disableAutoScroll;

	/**
	 * Launch the application.
	 */
	public static eActiveConsoledBar main() {
		final eActiveConsoledBar frame = new eActiveConsoledBar(373, 329);
		frame.setVisible(true);
		ThemeApplier.applyTheme(frame);
		frame.setLocation(
				(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getWidth()
						- frame.getWidth()) / 2,
				(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getHeight()
						- frame.getHeight()) / 2);
		return frame;
	}
	
	public static eActiveConsoledBar main(final int x, final int y) {
		final eActiveConsoledBar frame = new eActiveConsoledBar(x, y);
		frame.setVisible(true);
		ThemeApplier.applyTheme(frame);
		frame.setLocation(
				(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getWidth()
						- frame.getWidth()) / 2,
				(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getHeight()
						- frame.getHeight()) / 2);
		return frame;
	}
	
	

	private JProgressBar getBar() {
		return this.progressBar;
	}

	public void setMin(final int min) {
		this.progressBar.setMinimum(this.x = min);
	}

	public void setMax(final int max) {
		if (max > this.progressBar.getMinimum()) {
			this.progressBar.setMaximum(max);
		}
	}

	public void setStatus(final int value) {
		if (this.indter) {
			return;
		}
		this.progressBar.setValue(MathUtil.clamp(this.progressBar.getMinimum(), this.progressBar.getMaximum(), value));
	}

	/**
	 * Create the frame.
	 */
	public eActiveConsoledBar(final int x, final int y) {
		setIconImage(Toolkit.getDefaultToolkit().getImage(eActiveConsoledBar.class.getResource("/com/nulldev/util/java/swing/nullutil_logo.png")));
		setTitle("extSwing.eActiveConsoledBar");
		setResizable(false);
		if (ThemeApplier.useSystem) {
			try {
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			} catch (Exception e) {
			}
		}

		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, x, y);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		progressBar = new JProgressBar();

		lblLoading = new JLabel("Loading...");
		
		scrollPane = new JScrollPane();
		//SwingUtil.scrollToBottom(scrollPane);
		
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
						.addComponent(progressBar, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
						.addComponent(lblLoading, Alignment.LEADING))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(progressBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblLoading)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		textPane = new JTextPane() {
			
			@Override
			public void setFont(final Font font) {
				super.setFont(FontManager.getFonts("Monospaced", "mono"));
			}
			
		};
		textPane.setEditable(false);
		scrollPane.setViewportView(textPane);
		contentPane.setLayout(gl_contentPane);
		ThreadExecutor.execute(this.r);
	}

	public void setIndeterminate(final boolean state) {
		this.indter = state;
	}

	public JLabel getLabel() {
		return lblLoading;
	}

	public void end() {
		this.haltF = true;
		this.setVisible(false);
		this.dispose();
	}

	public void setText(final String string) {
		this.getLabel().setText(string);
	}
	
	public JTextPane getTextPane() {
		return textPane;
	}
	
	public void setConsoleText(final String text) {
		this.textPane.setText(text);
		SwingUtil.scroll(scrollPane, ScrollDirection.DOWN);
	}
	
	public void clearConsole() {
		this.textPane.setText("");
	}
	
	public void appendConsole(final String text) {
		if (this.textPane.getText() == null || this.textPane.getText().length() == 0) {
			this.textPane.setText(text);
		}else {
			this.textPane.setText(this.textPane.getText() + JVMUtil.lineSep() + text);
		}
		SwingUtil.scroll(scrollPane, ScrollDirection.DOWN);
	}

	public String appendConsoleB(final String text) {
		if (this.textPane.getText() == null || this.textPane.getText().length() == 0) {
			this.textPane.setText(text);
		}else {
			this.textPane.setText(this.textPane.getText() + JVMUtil.lineSep() + text);
		}
		SwingUtil.scroll(scrollPane, ScrollDirection.DOWN);
		return text;
	}

}
